package CloudDatabase.Project3.Group3.s12cs342;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import DataStructures.Project3.Group3.s12cs342.ScoreContainer;

public class ReadBlob {
	static String urls = "http://cs346.cs.uwosh.edu/students/gehrkp18/proj3cs342/readBlob.php?name=";

	
	public static ArrayList<ScoreContainer> retrieveBinaryData(String userName, String pass) {
		ArrayList<String> results = new ArrayList<String>();
		ArrayList<ScoreContainer> scoresResults = new ArrayList<ScoreContainer>();
		URL url = null;
		String url2 = urls + userName;
		BufferedReader br = null;

		try {
			try {
				// NEEDED VARIABLES URL READER LENGTH
				url = new URL(url2);

				// CONNECT SET AND REQUEST SET
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();

				connection.setUseCaches(false);
				connection.setDoOutput(true);
 
				br = new BufferedReader(new InputStreamReader(
						connection.getInputStream()));

				String temp;
					temp = br.readLine();
					while (temp != null) {
						results.add(temp);
						temp = br.readLine();
					}
					while (!results.isEmpty()){
						try{
						scoresResults.add(new ScoreContainer(results.remove(0), results.remove(0), results.remove(0), pass));
						}catch(IndexOutOfBoundsException e){
						}
					}

			} catch (MalformedURLException e) {
				System.out.println("URL incorrectly formed");
			} catch (IOException e) {
				System.out.println(e + "333");
			} finally {
				br.close();
			}
		} catch (Exception e1) {
			System.out.println(e1 + "444");
		}

		return scoresResults;
	}
}
